<?php
/*************************************
 *
 * @Controller 控制层主要接收与传递参数
 *
 ************************************/
namespace app\api\controller;

use app\common\services\IndexServices;

use think\Request;


class IndexController extends BaseController
{
    protected $IndexServices;
    protected $ImageServices;
    protected $RedisServices;
    protected $Request;

    public function __construct
    (
        IndexServices $IndexServices,
        RedisServices $RedisServices,
        Request $request
    )
    {
        parent::__construct($request);

        $this->IndexServices = new IndexServices();

        $this->RedisServices = new RedisServices();
    }

    /**
     *  随机获取redis心情词库
     *  return $msg String 词库数据
     */
    public function getMood()
    {
        $str =  $this->RedisServices->getMood();
        if (!$str) {
            $this->addMood();
            $str =  $this->RedisServices->getMood();
        }
        if ($str) {
            $this->successResult('200', $str);
        } else {
            return $this->errorHttpResult(['data'=>'暂无数据！', 'status_code' => 404], 404);
        }
    }

    /**
     *  存入词库数据到redis集合中
     *  return $msg String 词库数据
     */
    public function addMood()
    {
        $data =  $this->IndexServices->getAll();
        $this->RedisServices->addMood($data);
    }

    /**
     *  存入词库数据到redis集合中
     *  return $msg String 词库数据
     */

    public function getImg()
    {
        $url =  Env::get('IMG_PATH');
        $data['num'] = 29;
        $data['url'] = $url.'/Uploads/';
        $this->successResult('200', $data);
    }

    public function getRandom()
    {
        $data =  $this->IndexServices->getRandom();
        if ($data) {
            $this->successResult(200, $data);
        } else {
            return $this->errorHttpResult(['data'=>'暂无数据！', 'status_code' => 404], 404);
        }
    }

    public function defaultImg()
    {
        $data =  $this->IndexServices->defaultImg();
        if ($data) {
            $this->successResult(200, $data);
        } else {
            return $this->errorHttpResult(['data'=>'暂无数据！', 'status_code' => 404], 404);
        }
    }
}
